package rpg002;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;

/**
 *
 * @author Smai
 */
public class Map {

  public int mapSize = 256;
  private Tile[][] worldTiles = MapLoader.CreateMap(mapSize, mapSize, "map01");
  private ActionListener actionListener;
  private Player player;
  private MapEditor mapEditor;
  private UserInterface userInterface;
  //Muuttujat
  private int posFocusX;
  private int posFocusY;
  private int WorldArrayX;
  private int WorldArrayY;
  private String info = "info";

  public Map(Player player, ActionListener actionListener, MapEditor mapEditor, UserInterface userInterface) {
    this.actionListener = actionListener;
    this.player = player;
    this.mapEditor = mapEditor;
    this.userInterface = userInterface;
  }

  /*
   * Kaikki piirtäminen suoritetaan täälä
   */
  public void piirraKartta(Graphics gbi) {
    gbi.setColor(Color.black);
    gbi.fillRect(0, 0, 1024, 640);

    for (int i = 0; i < 36; i++) {
      for (int j = 0; j < 24; j++) {
        if (i + WorldArrayX >= 0 && j + WorldArrayY >= 0) {
          worldTiles[i + WorldArrayX][j + WorldArrayY].paint(gbi, i, j, posFocusX, posFocusY);
        }
      }
    }

    //Debuggausta
    gbi.setColor(Color.white);
    gbi.drawString("Editor on " + actionListener.buttonK, 16, 528);
    gbi.drawString("Focus " + posFocusX + "," + posFocusY, 16, 544);
    gbi.drawString("WorldArray " + WorldArrayX + "," + WorldArrayY, 16, 560);
    gbi.drawString("MousePos " + actionListener.mouseX + "," + actionListener.mouseY, 16, 576);
    gbi.drawString("[Inspect]: Tile contains " + info, 16, 592);
    gbi.drawString("Mouse " + actionListener.lmbClick + " " +actionListener.rmbClick, 16, 608);


  }

  public void moving(int x, int y) {
    if (collisionDetection(x, y)) {
      checkArray();
      return;
    } else {
      this.posFocusX = this.posFocusX + x;
      this.posFocusY = this.posFocusY + y;
      checkArray();
    }
  }

  public void checkArray() {

    //Tarkastetaan linjaukset x
    if (posFocusX == -32) {
      posFocusX = 0;
      WorldArrayX--;
    } else if (posFocusX == 32) {
      posFocusX = 0;
      WorldArrayX++;
    }

    //Tarkastetaan linjaukset Y
    if (posFocusY == -32) {
      posFocusY = 0;
      WorldArrayY--;
    } else if (posFocusY == 32) {
      posFocusY = 0;
      WorldArrayY++;
    }
  }

  public boolean collisionDetection(int x, int y) {
    Rectangle playerBoundaries = player.getRectangle(x, y);
    Rectangle blockBoundaries;

    for (int i = 0; i < 36; i++) { //36
      for (int j = 0; j < 24; j++) { //24
        if (i + WorldArrayX >= 0 && j + WorldArrayY >= 0) {
          blockBoundaries = worldTiles[i + WorldArrayX+15][j + WorldArrayY+9].getRectangle();
          if (blockBoundaries == null) {
          } else {
            if (playerBoundaries.intersects(blockBoundaries)) {
              return true;
            }
          }
        }
      }
    }
    return false;
  }

  public Tile[][] getWorldTiles() {
    return worldTiles;
  }

  public void checkMouseInfo(int x, int y) {
    Rectangle blockBoundaries;

    for (int i = 0; i < 36; i++) {
      for (int j = 0; j < 24; j++) {
        if (i + WorldArrayX >= 0 && j + WorldArrayY >= 0) {
          blockBoundaries = worldTiles[i + WorldArrayX][j + WorldArrayY].getRectangleForMouse();

          if (blockBoundaries.contains(x, y)) {
            //Editor modi on päällä
            if (actionListener.buttonK && !actionListener.rmbClick) {
              worldTiles[i + WorldArrayX][j + WorldArrayY] = mapEditor.setTile();

              //Editointi modi ei ole päällä
            } else{
              info = worldTiles[i + WorldArrayX][j + WorldArrayY].getInfo();
            }
            return;
          }
        }
      }
    }
  }
}
